﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

using EHTSaleManager.Code.BLL;
using EHTSaleManager.Code.Entity;
using EHTSaleManager.Code.Utility;

namespace EHTSaleManager.UICustomer
{
    public partial class CustomerOrder : System.Web.UI.Page
    {
        private Int32 categoryID = -1;
        private Int32 pageIndex = 1;
        private Int32 pageSize = 10;
        private Int32 totalRecord;

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                categoryID = Int32.Parse(Request.QueryString["CategoryID"].ToString());
            }
            catch (Exception ex)
            {
                categoryID = -1;
                Form.Action = "CustomerOrder.aspx";
            }

            try
            {
                pageIndex = Int32.Parse(Request.QueryString["PageIndex"].ToString());
            }
            catch (Exception)
            {
                pageIndex = 1;
            }

            pageSize = this.PaggingControl1.PageSize;

            if (!Page.IsPostBack)
            {
                GetAllCategorys();
                if (categoryID != -1)
                {
                    ddlCategory.SelectedIndex = ddlCategory.Items.IndexOf(ddlCategory.Items.FindByValue(categoryID.ToString()));
                }
                GetProductsByCategoryID(Int32.Parse(ddlCategory.SelectedValue));
            }
        }

        private void GetAllCategorys()
        {
            DataSet dsCategorys = CategoryProvider.GetCategorys();
            ddlCategory.DataSource = dsCategorys;
            ddlCategory.DataTextField = "CategoryName";
            ddlCategory.DataValueField = "CategoryID";
            ddlCategory.DataBind();
            ddlCategory.Items.Insert(0, new ListItem("-- All --", "-1"));
        }

        private void GetProductsByCategoryID(Int32 categoryID)
        {
            DataSet dsProduct = ProductProvider.GetProductsByCategoryID(categoryID, pageIndex, pageSize, out totalRecord);
            this.rpProducts.DataSource = dsProduct;
            this.rpProducts.DataBind();

            this.PaggingControl1.PageIndex = pageIndex;
            this.PaggingControl1.TotalRecord = totalRecord;
            this.PaggingControl1.PageURL = "CustomerOrder.aspx?CategoryID=" + categoryID.ToString();
        }

        protected void rpProducts_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            String cmdName = e.CommandName;
            Int32 productID = Int32.Parse(e.CommandArgument.ToString());
            ShoppingCart.Instance.AddItem(productID);
            Response.Redirect("CustomerViewCart.aspx");
        }

        protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetProductsByCategoryID(Int32.Parse(ddlCategory.SelectedValue));
        }
    }
}